package com.xiangban.volunteer_platform.controller;


import com.xiangban.volunteer_platform.entity.Ids;
import com.xiangban.volunteer_platform.entity.User;
import com.xiangban.volunteer_platform.entity.dto.LeaderIdDto;
import com.xiangban.volunteer_platform.service.UserService;
import com.xiangban.volunteer_platform.utils.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/1/27 12:38
 */
@RestController
@RequestMapping("/api/volunteer_user")
@Api(tags = "志愿者控制器")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/addSave")
    @ApiOperation("添加志愿者")
    public JsonData addSave(@RequestBody User user){
        int save = userService.save(user);
        if (save == -1){
            return JsonData.buildError("用户名重复！");
        }
        return JsonData.buildSuccess(user.getUserId());
    }

    @PostMapping("/updateUser")
    @ApiOperation("修改志愿者")
    public JsonData updateUser(@RequestBody User user){
        int update = userService.update(user);
        if (update == -1){
            return JsonData.buildError("操作失败，无此人信息");
        }
        return JsonData.buildSuccess("操作成功");
    }

    @PostMapping("/delUser")
    @ApiOperation("删除志愿者")
    public JsonData delUser(@RequestBody User user){
        userService.del(user);
        return JsonData.buildSuccess();
    }

    @PostMapping("/getAllUser")
    @ApiOperation("分页获取志愿者列表")
    public JsonData getAllUser(@RequestParam(required = false) Integer pageNum,
                               @RequestParam(required = false) Integer pageSize,@RequestBody User user){
        return JsonData.buildSuccess( userService.getAll(pageNum,pageSize,user));
    }


    @PostMapping("/getUserByOpenId")
    @ApiOperation("根据OpenId获取对象")
    public JsonData getUserByOpenId(@RequestBody User user){
       return JsonData.buildSuccess(userService.getUser(user));

    }

    @PostMapping("/getUserById")
    @ApiOperation("根据id查询对象")
    public JsonData getUserById(@RequestBody User user){
        return JsonData.buildSuccess(userService.getUserById(user));
    }

    @PostMapping("/infoPhone")
    @ApiOperation("判断手机号")
    public JsonData infoPhone(@RequestBody User user){
        Object o = userService.byBhone(user);
        if (o.equals(-1)){
            return JsonData.buildError("无手机号重复");
        }
        return JsonData.buildSuccess(o);
    }

    @PostMapping("/getUserList")
    @ApiOperation("获取类型为志愿者列表")
    public JsonData getUserList(){
      return JsonData.buildSuccess(userService.getList());
    }

    @PostMapping("/getUserListById")
    @ApiOperation("获取小组长以外的志愿者")
    public JsonData getUserListById(@RequestBody Ids ids){
       return JsonData.buildSuccess( userService.getListById(ids));
    }

    @PostMapping("/resetting")
    @ApiOperation("重置归属小组长id")
    public JsonData resetting(@RequestBody LeaderIdDto leaderIdDto){
        int rows = userService.resetting(leaderIdDto);
        return rows == -1 ? JsonData.buildError("请传入正确参数") : JsonData.buildSuccess("重置成功");
    }
    @PostMapping("/PersonListsAndLeaderIdList")
    @ApiOperation("重新分配小组长id")
    public JsonData updatePersonListsByLeaderIdList(@RequestBody LeaderIdDto leaderIdDto){
        int rows = userService.updatePersonListsByLeaderIdList(leaderIdDto);
        return rows == -1 ? JsonData.buildError("请传输正确参数") : JsonData.buildSuccess("修改成功");
    }

}